import QtQuick 2.9
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.12

import "./layouts" as Layouts
import "./widgets" as Widgets

Item {
    id: root
    width: parent.width
    height: parent.height
    focus: true


    /////////////////
    function showAboutDialog() {
        dlgAbout.show()
    }

    function setBoxConnectState(state) {
        detectForm.setBoxConnectState(state)
    }

    function showDetectConfirm() {
        confirmDialog.show("開始檢測", qsTr("是否已經在滴入樣品後等待 10 分鍾？（提前讀取數據會造成檢測結果失準）"))
    }

    ////////////////////////////////////////
    Component.onCompleted: {
    }

    Keys.onPressed: {
        console.log("press key:" + event.key)
        if(event.key === Qt.Key_Back) {
            event.accpted = true
            stackView.back()
        }
    }

    //.......................... TOP ....


    //........................ CONTENT ....
    StackView {
        id: stackView
        width: parent.width
        height: parent.height
        initialItem: detectForm

        DetectStepForm {
            id: detectForm
            onRequestShowConnectDevice: {
                stackView.push(scanBoxForm)
            }

            onRequestShowDetect: {
                stackView.push(scanForm) //reportForm
            }

            onRequestShowHistory: {
                stackView.push(historyForm)
            }
        }

        onDepthChanged: {
        }

        function back() {
            if (depth>1) {
                stackView.pop()
            }
        }

        function goHome() {
            console.log("clicked goHome()...depth="+depth)
            while (depth > 1) {
                stackView.pop()
                console.log("clicked goHome()...pop")
            }
        }
    }

    Component {
        id: scanBoxForm
        ScanDeviceForm {
            onBack: stackView.back()
            onBackWithCode: {
                g_currentBox = code
                stackView.back()

                setBoxConnectState(1)
            }
        }
    }

    Component {
        id: scanForm
        ScanForm {
            onBack: stackView.back()

            onRequestShowSacnBarcode: {
                stackView.push(scanBarcodeForm)
            }

            onBackWithCode: {
                console.log("barcode:" + code)
                stackView.goHome()
                showDetectConfirm()
            }
        }
    }

    Component {
        id: scanBarcodeForm
        ScanBarCodeForm {
            onBack: stackView.back()
            onBackWithCode: {
                console.log("barcode.2:" + code)
                //TODO show receiving data dialog

                stackView.goHome()
                showDetectConfirm()
            }
        }
    }

    Component {
        id: historyForm
        HistoryForm {
            onBack: stackView.back()
            onShowReport: {
                stackView.push(reportForm, {"reportId": title, "reportTime": time})
            }
        }
    }

    Component {
        id: reportForm
        ReportForm {
            onBack: stackView.back()
        }
    }


    //////////////

    AnalyseForm {
        id: analyseForm
        anchors.fill: parent
        visible: false

        onAnalyseSuccess: {
            stackView.push(reportForm)
        }
    }

    Widgets.ConfirmDialog {
        id: confirmDialog
        anchors.fill: parent
        visible: false

        onClickedYes: {
            analyseForm.show(g_currentBox)
        }

        onClickedNo: {
        }
    }

    Widgets.MsgBox {
        id: msgBox
        anchors.fill: parent
        visible: false
    }

    Widgets.AboutDialog {
        id: dlgAbout
        anchors.fill: parent
        visible: false
    }
}
